package com.fanhao.dllo.justforyou.activity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.bartoszlipinski.recyclerviewheader.RecyclerViewHeader;
import com.fanhao.dllo.justforyou.R;
import com.fanhao.dllo.justforyou.adapter.FoodHouseInforAdapter;
import com.fanhao.dllo.justforyou.entity.FoodHouseInforResult;
import com.fanhao.dllo.justforyou.volley.GetVolleyDatasUtil;
import com.fanhao.dllo.justforyou.volley.VolleySingleton;
import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;

public class FoodHouseInforActivity extends AppCompatActivity implements View.OnClickListener {
    private RecyclerViewHeader recycleViewHeader;
    private RecyclerView recyclerView;
    private FoodHouseInforAdapter foodHouseInforAdapter;

    private ImageView backIv,imgIv;
    private TextView nameTv,poi_dist,address,phone;

//    private FoodHouseInforResult.Manchant manchant ;

    private int position;
    private String id;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_food_house_infor);

        getData();
        initView();
        initData();
    }



    private void initView() {
        nameTv = (TextView)findViewById(R.id.nameTv);
        poi_dist = (TextView)findViewById(R.id.poi_dist);
        address = (TextView)findViewById(R.id.address);
        phone = (TextView)findViewById(R.id.phone);
        imgIv = (ImageView)findViewById(R.id.imgIv);

        recycleViewHeader = (RecyclerViewHeader)findViewById(R.id.recyclerViewheader);


        backIv = (ImageView)findViewById(R.id.backIv);
        backIv.setOnClickListener(this);

    }

    private void getData() {
        Intent intent = getIntent();

        id = intent.getStringExtra("id");
        Log.d("FoodHouseInforActivity", id);

    }
    private void initData() {
        String url = "http://t10.nuomi.com/webapp/na/merchant?merchantId="+id+"&location=38.889931%2C121.550997&bduss=Wx-S2d1THktaFJZT1lWYlBDRXVJTU0yZklUTEgtR1o2aH5iRVhwSWdIeVZEd2xXQVFBQUFBJCQAAAAAAAAAAAEAAABmXqktYmFiec7SsK7D3ruozMcAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAJWC4VWVguFVQ&s=83140966d83b3d9030d55f937e3cdc27&cityId=1300010000&cuid=04D245A971F5A6AE19141F6507D49342%7C494550320493668";
        StringRequest request = new StringRequest(url,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        Gson gson = new Gson();
                        try {
                            JSONObject object = new JSONObject(response);
                            if (object.has("manchant")) {
                                JSONObject object1 = object.getJSONObject("manchant");
                                String str = object1.toString();
                                FoodHouseInforResult.Manchant manchants = gson.fromJson(str, FoodHouseInforResult.Manchant.class);
                                nameTv.setText(manchants.getPoi_name());
                                poi_dist.setText(manchants.getPoi_dist());
                                address.setText(manchants.getAddress());
                                phone.setText(manchants.getPhone());
                                String url = manchants.getImage();
                                GetVolleyDatasUtil.getBitmapRequest(url,imgIv);
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }


                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {

                    }
                });
        VolleySingleton.getInstance().addRequest(request);
    }

    @Override
    public void onClick(View v) {
        finish();
    }
}
